/*
 * Achieve it All! - Android app to motivate achieving your goals
 * Copyright (C) 2014  Xilconic
 * 
 * This file is part of Achieve it All!.
 *
 * Achieve it All! is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Achieve it All! is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Achieve it All!.  If not, see <http://www.gnu.org/licenses/>
 */
package com.xilconic.achieveitall.domain;

import java.util.ArrayList;
import java.util.List;

import android.os.Parcel;
import android.os.Parcelable;

public class GoalsOverview implements Parcelable{
	ArrayList<Goal> inactiveGoals;
	ArrayList<Goal> activeGoals;
	
	public GoalsOverview(){
		activeGoals = new ArrayList<Goal>();
		inactiveGoals = new ArrayList<Goal>();
	}

	public GoalsOverview(Parcel in) {
		inactiveGoals = new ArrayList<Goal>();
		in.readTypedList(inactiveGoals, Goal.CREATOR);
		
		activeGoals = new ArrayList<Goal>();
		in.readTypedList(activeGoals, Goal.CREATOR);
	}

	/**
	 * Returns all the {@link Goal} objects accepted by the user.
	 * @return All accepted goals.
	 */
	public List<Goal> getActiveGoals() {
		return activeGoals;
	}

	/**
	 * Returns all {@link Goal} objects available to the user.
	 * @return All available goals.
	 */
	public List<Goal> getInactiveGoals() {
		return inactiveGoals;
	}

	/**
	 * Adds a goal to the inactive list.
	 * @param goal The goal to be added.
	 */
	public void addGoal(Goal goal) {
		inactiveGoals.add(goal);
	}

	/**
	 * Accepts a {@link Goal} from the inactive list and moves it to the active
	 * list.
	 * @param inactiveGoalIndex Goal's index from inactive goal list. 
	 */
	public void acceptGoal(int inactiveGoalIndex) {
		if(inactiveGoalIndex < 0 || inactiveGoalIndex >= inactiveGoals.size()){
			String errorMessage = createTargetIndexOutOfRangeMessage(inactiveGoalIndex);
			throw new IllegalArgumentException(errorMessage);
		}
		Goal acceptedGoal = inactiveGoals.remove(inactiveGoalIndex);
		activeGoals.add(acceptedGoal);
	}
	
	private String createTargetIndexOutOfRangeMessage(int targetIndex) {
		StringBuilder builder = new StringBuilder();
		builder.append("inactiveGoalIndex (");
		builder.append(targetIndex);
		builder.append(") must be in range of the collection inactiveGoals (size = ");
		builder.append(inactiveGoals.size());
		builder.append(").");
		String errorMessage = builder.toString();
		return errorMessage;
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel parcel, int flags) {
		parcel.writeTypedList(inactiveGoals);
		parcel.writeTypedList(activeGoals);
	}
	
	/**
	 * Object creator to create a {@link GoalsOverview} from {@link Parcel}
	 * contents.
	 */
    public static final Parcelable.Creator<GoalsOverview> CREATOR
	    = new Parcelable.Creator<GoalsOverview>() {
		public GoalsOverview createFromParcel(Parcel in) {
		    return new GoalsOverview(in);
		}
		
		public GoalsOverview[] newArray(int size) {
		    return new GoalsOverview[size];
		}
    };

	
}
